javascriptfandomcom-20200222-history
Object
A JavaScript object is a container of key-value pairs, known as 'maps', 'dictionaries', or 'associative arrays' in other languages. The keys are always strings, and lookups can be performed with the identifier lookup or string lookup operators. The Object is fundamental to Javascript, and is the basis of all other non-primitive types, as well as being vital to JavaScript's object-orientation model. Excluding object representations of primitives, all objects are represented as 'object' by typeof. Literal syntax Definition An object is created by surrounding a list of comma-separated key : value pairs with braces. The key must be a quoted string or an identifier (which will be converted to a string); the value can be any expression. Caveat: In positions where a block is allowed, objects must be surrounded with parentheses to avoid ambiguity, or errors will result. #({ foo : 5 + 3 }) // Equivalent to #2 #({ "foo" : 5 + 3 }) // Equivalent to #1 #// ({ foo&bar : 5 }) // Illegal #({ "foo&bar" : 5 }) // Legal Operators Lookup operators Since objects are defined as a collection of key/value pairs, there must be operators to get and set those pairs. These are the lookup operators, and they are specific to objects (although primitive values are usually converted to an appropriate object instance when a lookup operator is applied). Lookup expressions form a sort of variable; it's possible to assign to them, setting the property. New properties can be created simply by assigning to a non-existent property. The delete operator must be used to completely remove a key from an object. Attempting to look up a non-existent property will yield undefined. . (identifier lookup) If the name of a property is a valid identifier, it can be looked up by providing the object and the identifier directly (unquoted) to the . operator. ({ foo: 5 }).foo // 5 ({}).foo // undefined [] (string lookup) It is also possible to supply a string value using the string lookup operator. This allows access of properties via dynamically-generated names, such as iterations of a for..in loop. Unlike the identifier lookup, the name need not be a valid identifier; this is most often used in arrays, where the property names are numerical. ({ foo: 5 })'foo' // 5 ({ "1 invalid identifier!" : "fish" })invalid identifier!' // "fish" Comparison operators These operators take two objects and return a boolean value. (equals) Returns true if both operands are identical (references to the same object); otherwise, returns false. It does not compare structure: two different objects that are structurally identical will not be considered equal. ({ foo: 5 }) ({ foo: 5 }) // false: these two are not the same object! var a = { foo: 5 }, b = a; a b // true: a and b are references to the same object. ! (not equals) Checks whether objects are non-identical. a ! b is equivalent to !(a b). ({ foo: 5 }) ! ({ foo: 5 }) // true var a = { foo: 5 }, b = a; a ! b // false Members prototype |documentation=Mozilla, Microsoft }} (This includes object literals.) constructor |documentation=Mozilla, Microsoft }} Points to the function used to construct the object. For direct Object instances, this will be Object. Caveat: Not available in WebKit/KHTML. toString |documentation=Mozilla Microsoft }} Returns a string representation of the object. Default is "ConstructorName", where ConstructorName is the name of the constructor that created this object. hasOwnProperty |documentation=Mozilla, Microsoft }} Checks whether the object itself (as opposed to a prototype) has a property associated with the given key. isPrototypeOf |documentation=Mozilla Microsoft }} Returns true if this object is the prototype of the provided obj; otherwise, returns false. Category:Literal types